package org.bootstrap.route;


import cn.hutool.core.lang.Console;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import org.bootstrap.config.ApplicationConfiguration;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

//@WebFilter(filterName = "web-filter", asyncSupported = true, urlPatterns = "/*", dispatcherTypes = {
//        DispatcherType.REQUEST,
//        DispatcherType.FORWARD,
//        DispatcherType.INCLUDE,
//        DispatcherType.ERROR,
//        DispatcherType.ASYNC
//})
public class AuthRouterFilter implements Filter {
    @Resource
    ApplicationConfiguration configuration;

    private ArrayList<String> blacklist; // 黑名单

    private ArrayList<String> whitelist; // 白名单


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        blacklist = configuration.getIncludes();
        whitelist = configuration.getExcludes();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding("UTF-8");
        String origin = request.getHeader("origin");
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Content-Type","application/json;charset=utf-8");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Allow-Headers", "[Content-Type, Authorization, X-Requested-With]");

        String uri = request.getRequestURI(); // 请求路径
        Console.log("路径",uri);


        Cookie[] req_token = request.getCookies();
        Console.log("token",req_token);
        /*
        if (BooleanUtil.isTrue(configuration.isEnable())) {
            boolean secure = request.isSecure();
            if (BooleanUtil.isTrue(secure) ) {
                Console.log("路径参数", uri);
                if (whitelist.contains(uri)) { // 白名单放行
                    Console.log("放行", uri);
                    response.setStatus(200);
                }
            } else {
                response.sendError(402, "没有权限访问");
            }
        } else {
            Console.error("未开启", uri);
            if (StrUtil.isNotEmpty(uri)) {
                String token = request.getHeader("Token");
                Console.log("token", token);
                response.setStatus(200);
            }
        }
         */
        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
